<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dosen extends Controller {

	function Dosen()
	{
		parent::Controller();
		$this->load->library('auth');
		$this->load->library('fungsi');
		$this->load->model(array('setting_m', 'area_m','app_login_auth','Dosen_Model','Admin_Model'));
	}	
	function index()
	{ 
		$this->data['graph'] = $this->creategraph();
		if ($this->session->userdata('login') == false)
		{
			redirect('home');
		}
		else if ( $this->session->userdata('login') == true && $this->session->userdata('akses') == 1)
		{
			redirect('admin');
		}
		else if ( $this->session->userdata('login') == true && $this->session->userdata('akses') == 3)
		{
			//Definisi untuk memanggil function di libraries helper
			$this->load->helper('app');
			
			//Mengambil 10 data koleksi terbaru yang di upload
			$this->data['newUpload'] = $this->setting_m->getNewUpload('');
			//content body
			$this->data['content'] = 'dosen/home_v';
			$this->data['left'] = 'bg_left';
			$this->data['footer'] = 'footer';
			$this->data['account'] = $this->setting_m->getUploadFromTypeCollection();
			//header
			$this->data['header'] = 'bg_header';
			//Mengambil 10 data jenis koleksi yang baru diupload
			$this->data['newTypeUpload'] = $this->setting_m->getUploadFromTypeCollection();
			//Mengambil 10 data yang harus di approve
			$this->data['tableUploadPublish'] = $this->getTableTopTenPublish();
			//Memanggil View
			$this->load->view('home/index', $this->data);
		}
		else if ( $this->session->userdata('login') == true && $this->session->userdata('akses') == 4)
		{
			redirect('mahasiswa');
		}
	}
	
	function getTableTopTenPublish()
	{
		$getTenPublish = $this->Dosen_Model->get_ten_approve();
		$dtTable = '';
		foreach ($getTenPublish as $getToPublish)
		{
			// Penyusunan data baris per baris, perhatikan pembuatan link untuk updat dan delete
			// $this->table->add_row(++$i, $getToPublish->TITLE, $getToPublish->NAMA, 
									// anchor('admin/appvAdvisor/'.$getToPublish->ID_COLLECTION,$getToPublish->Approve,array('class' => 'update',
																									// 'onclick'=>"return confirm('Anda yakin akan approve data ini?')")),anchor('admin/Publish/'.$getToPublish->ID_COLLECTION,$getToPublish->Publish,array('class' => 'update',
																									// 'onclick'=>"return confirm('Anda yakin akan approve data ini?')"))
								// );
			$dtTable .= '<tr>
							<td headers="t1" class="standard">'.$getToPublish->TITLE.'</td>
							<td headers="t2" class="standard">'.$getToPublish->NAMA_PENULIS.'</td>
							<td headers="t3" class="standard">';//.$getToPublish->NAMA.'</td>
			$dtTable .= anchor('admin/appvAdvisor/'.$getToPublish->ID_COLLECTION,$getToPublish->APPROVE,array('class' => 'update','onclick'=>"return confirm('Anda yakin akan approve data ini?')"));
			$dtTable .=	'</td></tr>';
		}
	
		return $dtTable;
	}
	
	function getTableApprove()
	{
		$DosenApprove = $this->Dosen_Model->get_ten_approve();
		
		// Set template tabel, untuk efek selang-seling tiap baris
		$tmpl = array( 'table_open'    => '<table border="0" cellpadding="0" cellspacing="0">',
					  'row_alt_start'  => '<tr class="zebra">',
						'row_alt_end'    => '</tr>'
					  );
		$this->table->set_template($tmpl);

		// Set heading untuk tabel
		$this->table->set_empty("&nbsp;");
		$this->table->set_heading('No', 'Title', 'Author', 'Approve');
		
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		
		// Penomoran baris data
		$i = 0 + $offset;
		
		foreach ($DosenApprove as $newRegist)
		{
			// Penyusunan data baris per baris, perhatikan pembuatan link untuk updat dan delete
			$this->table->add_row(++$i,$newRegist->TITLE, $newRegist->NAMA_PENULIS, anchor('dosen/approve/'.$newRegist->ID_COLLECTION,$newRegist->APPROVE,array('class' => 'aktif','onclick'=>"return confirm('Anda yakin akan approve data ini?')"))
								);
		}
		$data['tableDosenApprove'] = $this->table->generate();
		
		return $data['tableDosenApprove'];
	}
	
	function approve($id)
	{
		
		try
		{
			if ( $this->Dosen_Model->update($id) == true){
				redirect('dosen');
			}
			else
			{
				redirect('dosen');
			}
		}
		catch (Exception $e)   // You throwed your custom exception with the failed validation information
		{
			// Do something with your custom exception like the kohana example
			//Memanggil View
			$this->data['error'] = 'Gagal disimpan';
			$this->load->view('dosen/insert', $this->data);
		}
	}
	
	function do_Saving()
	{
		$Title       = $this->input->post('txtTitle', TRUE);
        $Abstract    = $this->input->post('txtAbstract', TRUE);
		$jnsKoleksi  = $this->input->post('ddlJenisKoleksiInsert', TRUE);
		$noAuthor = $this->input->post('txtNoAuthor', TRUE);
		$nameAuthor = $this->input->post('txtAuthor', TRUE);
		$noAdvisor = $this->input->post('txtNoAdvisors', TRUE);
		$nameAdvisor = $this->input->post('txtAdvisor', TRUE);
		$jurusan  = $this->input->post('ddlJurusanInsert', TRUE);
		$tglRealese  = $this->input->post('dtRelase', TRUE);
		
		$this->load->helper('app');
		$this->data['criteria'] = buildCriteriaDropdown('', '');
		$this->data['operator'] = buildOperatorDropdown('', '');
		$this->data['operCrit'] = buildOpCriteriaDropdown('', '');
		
		//Untuk Binding data ke dropdownlist
		//'ddlJenisKol' dikirim ke view untuk memasukan data ke dalam dropdownlist
		$this->data['ddlJenisKol'] = $this->setting_m->getCboJenisKoleksi();
		$this->data['ddlJurusan'] = $this->setting_m->getCboJurusan();
		
		//Mengambil 10 data koleksi terbaru yang di upload
		$this->data['newUpload'] = $this->setting_m->getNewUpload('');
		$this->data['newTypeUpload'] = $this->setting_m->getUploadFromTypeCollection();
		
		
		try
		{
			if ( $result = $this->setting_m->SavedCollection($Title, $Abstract, $jnsKoleksi, $noAuthor, $nameAuthor, $noAdvisor, $nameAdvisor, $jurusan, $tglRealese) == true)
			{
				$this->data['error'] = 'Berhasil disimpan';
				$this->load->view('dosen/insert', $this->data);
			}
			
		}
		catch (Exception $e)   // You throwed your custom exception with the failed validation information
		{
			// Do something with your custom exception like the kohana example
			//Memanggil View
			$this->data['error'] = 'Gagal disimpan';
			$this->load->view('dosen/insert', $this->data);
			
		}
	}
	
	function creategraph()
	{
		$this->load->library('graph');
		$data_1 = array('30','20');
		// for( $i=0; $i<7; $i++ )
		// {
			// $data_1[] = rand(30,0);
		// }
		$ff = new graph();
		$ff->set_data( $data_1 );
		$ff->title( 'Trafik Jumlah Koleksi', '{font-size: 14px; color: #3D5570;font-family:calibri;}' );
		$ff->line_dot( 3, 5, '#8B6122', 'Repository', 10 );
		$ff->bg_colour = '#FFFFFF';
		$ff->x_axis_colour( '#818D9D', '#ADB5C7' );
		$ff->y_axis_colour( '#818D9D', '#ADB5C7' );
		$ff->set_x_labels( array( '2013') );
		$ff->set_y_max( 600 );
		$ff->y_label_steps( 10 );
		$ff->set_y_legend( 'Jumlah Koleksi', 12, '#3D5570' );
		$ff->set_x_legend( 'Tahun', 12, '#3D5570' );
		$ff->set_output_type('js');
		$ff->width = '20%';
		$ff->height = '20%';
		return $ff->render();
	}
	
}